package com.google.coffeeshop.gui.desktop.orderpayment.seatdetail;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import vn.bang.utility.FileUtil;
import vn.bang.utility.IntegerUtil;
import vn.bang.utility.LongUtil;

import com.google.coffeeshop.business.LoadingBusiness;
import com.google.coffeeshop.business.ManagerBusiness;
import com.google.coffeeshop.business.OrderBusiness;
import com.google.coffeeshop.entity.OrderLineDTO;
import com.google.coffeeshop.entity.ProductDTO;
import com.google.coffeeshop.gui.desktop.ObservingTextField;
import com.google.coffeeshop.gui.desktop.SwingFactory;
import com.google.coffeeshop.gui.desktop.selfcomponent.ImagePanel;
import com.google.coffeeshop.gui.desktop.selfcomponent.LRItem;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class EditOrderLineFrame extends JFrame{	
	private static final long serialVersionUID = 1L;
	private ImagePanel pnlImage;

	JLabel lblProductTitle;
	private ObservingTextField tfPrice;
	private List<ProductDTO> listProducts;
	private JTextField tfQuanity;
	private SeatDetailFrame parent;
	private OrderLineDTO originalOrderLine;
	private JTextField tfProduct;
	private long productId;

	public EditOrderLineFrame(SeatDetailFrame parent, long orderLineId) {
		setSize(900, 400);
		setTitle("Update Order Line");
		originalOrderLine = OrderBusiness.findOrderLineById(orderLineId);
		productId = originalOrderLine.getProduct().getId();
		this.parent = parent;
		super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		initLayout();
		displayOrderLineDetails(orderLineId);
	}
	
	private void displayOrderLineDetails(long orderLineId) {
		OrderLineDTO orderLine = originalOrderLine;
		setProductOnGUI(orderLine);
		tfQuanity.setText(String.valueOf(orderLine.getQuantity()));
	}

	public void setProductOnGUI(OrderLineDTO orderLine) {
		tfProduct.setText(orderLine.getProduct().getName());
		tfPrice.setText(String.valueOf(orderLine.getPriceOfUnit()));
		pnlImage.setImage(FileUtil.getRelativePathFromProjectPath("/src/main/java/com/google/coffeeshop/gui/desktop/images/products/" + orderLine.getProduct().getImageFilename()));
	}
	
	public void maximizeScreen() {
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
	}
	
	public void restoreScreen() {
		this.setExtendedState(JFrame.NORMAL);
	}
	
	private void updateOrder() {
		OrderLineDTO modifiedOrderLine = getOrderLineDTO();
		OrderBusiness.returnStuff(
				originalOrderLine.getOrder().getSeat().getId(), 
				originalOrderLine.getOrder().getEmployee().getId(), 
				originalOrderLine.getProduct().getId(), 
				originalOrderLine.getQuantity());
		OrderBusiness.orderStuff(
				modifiedOrderLine.getOrder().getSeat().getId(), 
				modifiedOrderLine.getOrder().getEmployee().getId(), 
				modifiedOrderLine.getProduct().getId(), 
				modifiedOrderLine.getQuantity());
		refreshParentGUI();
		disposeFrame();
	}

	private OrderLineDTO getOrderLineDTO() {
		if (productId == originalOrderLine.getProduct().getId()) {
			OrderLineDTO orderLine = OrderBusiness.findOrderLineByProductId(originalOrderLine.getOrder().getId(), productId);
			orderLine.setQuantity(LongUtil.parseLong(tfQuanity.getText()));
			return orderLine;
		}
		
		ProductDTO product = OrderBusiness.findProduct(productId);
		OrderLineDTO orderLine = OrderBusiness.findOrderLineByProductId(originalOrderLine.getOrder().getId(), productId);
		if (orderLine == null) {
			OrderBusiness.orderStuff(originalOrderLine.getOrder().getSeat().getId(), originalOrderLine.getOrder().getEmployee().getId(),
					productId, IntegerUtil.parseInt(tfQuanity.getText()));
		}
		orderLine.setProduct(product);
		orderLine.setOrder(originalOrderLine.getOrder());
		orderLine.setPriceOfUnit(ManagerBusiness.findLatestPriceById(product.getId()).getPrice());
		orderLine.setQuantity(LongUtil.parseLong(tfQuanity.getText()));
		return orderLine;

	}
	private void refreshParentGUI() {
		parent.refreshParentGUI();
	}
	private void disposeFrame() {
		this.dispose();
	}

	private void initLayout(){
		setSize(600, 335);
		getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(97dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(39dlu;default)"),
				ColumnSpec.decode("max(7dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(115dlu;default):grow"),
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.PREF_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(31dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		lblProductTitle = new JLabel();
		lblProductTitle.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblProductTitle.setHorizontalAlignment(SwingConstants.CENTER);
		getContentPane().add(lblProductTitle, "2, 2, 9, 1, center, default");
		
		pnlImage = SwingFactory.createImagePanel(Boolean.TRUE);
		pnlImage.setImage(FileUtil.getRelativePathFromProjectPath("/src/main/java/com/google/coffeeshop/gui/desktop/images/products/" + SwingFactory.NO_PRODUCT_IMAGE_NAME));
		getContentPane().add(pnlImage, "2, 4, 2, 13, fill, fill");
		setProductModel();
		
		JLabel lblProduct = new JLabel("Product");
		getContentPane().add(lblProduct, "4, 6, right, default");
		
		tfProduct = new JTextField();
		tfProduct.setEditable(false);
		getContentPane().add(tfProduct, "7, 6, fill, top");
		tfProduct.setColumns(10);
		
		JLabel lblPrice = new JLabel("Price");
		getContentPane().add(lblPrice, "4, 10, right, default");
		
		tfPrice = new ObservingTextField();
		tfPrice.setEditable(false);
		getContentPane().add(tfPrice, "7, 10, fill, default");
		tfPrice.setColumns(10);
		
		JLabel lblQuantity = new JLabel("Quantity");
		getContentPane().add(lblQuantity, "4, 12, right, default");
		
		tfQuanity = new JTextField();
		getContentPane().add(tfQuanity, "7, 12, fill, default");
		tfQuanity.setColumns(10);
		
		JButton btnUpdate = new JButton("Update");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateOrder();
			}
		});
		getContentPane().add(btnUpdate, "2, 22, 6, 1, fill, fill");
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				disposeFrame();
			}
		});
		getContentPane().add(btnCancel, "8, 22, left, fill");
	}
	
	private void changeProduct(LRItem item) {
		long productId = LongUtil.parseLong(item.getLeftText());
		ProductDTO product = OrderBusiness.findProduct(productId);
		tfPrice.setText(String.valueOf(ManagerBusiness.findLatestPriceById(productId).getPrice()));
		pnlImage.setImage(FileUtil.getRelativePathFromProjectPath("/src/main/java/com/google/coffeeshop/gui/desktop/images/products/" + product.getImageFilename()));
	}
	
	private void setProductModel(){
		DefaultComboBoxModel<LRItem> model = new DefaultComboBoxModel<LRItem>();
		listProducts = LoadingBusiness.getAllProducts();
		for(ProductDTO product: listProducts){
			model.addElement(new LRItem(String.valueOf(product.getId()), product.getName()));	
		}
	}
	
}
